package com.zzjson.serial;

import java.io.*;
import java.util.ArrayList;
import java.util.Arrays;

/**
 * <p>****************************************************************************</p>
 * <p><b>Copyright © 2010-2020 shuncom team All Rights Reserved<b></p>
 * <ul style="margin:15px;">
 * <li>Description : com.zzjson.serial</li>
 * <li>Version     : 1.0.0</li>
 * <li>Creation    : 2020年02月19日</li>
 * <li>@author     : zzy0_0</li>
 * </ul>
 * <p>****************************************************************************</p>
 */
public class JavaFileSerializer implements ISerializer {

	@Override
	public <T> byte[] serialize(T obj) {
		try {
			ObjectOutputStream objectOutputStream = new ObjectOutputStream(new FileOutputStream(new File("user")));
			objectOutputStream.writeObject(obj);
		} catch (IOException e) {
			e.printStackTrace();
		}

		return new byte[0];
	}

	@Override
	public <T> T deserialize(byte[] data, Class<T> clazz) {
		try {
			ObjectInputStream objectInputStream = new ObjectInputStream(new FileInputStream(new File("user")));

			try {
				return (T) objectInputStream.readObject();
			} catch (ClassNotFoundException e) {
				e.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}

	public static void main(String[] args) {
		User user = new User();
		user.setFatherName("fatherName");
		user.setName("zzy");
		user.setAge(11);
		user.setNames(Arrays.asList("1", "2", "3"));
		new JavaFileSerializer().serialize(user);
		User deserialize = new JavaFileSerializer().deserialize(null, User.class);
		System.out.println(deserialize);
		System.out.println(deserialize.getFatherName());

		ThingsObjectDataEventsMessageDTO thingsObjectDataEventsMessageDTO = new ThingsObjectDataEventsMessageDTO();
		thingsObjectDataEventsMessageDTO.setAlarmAction("ds");
		ThingsObjectDataEventsMessageDTO.EventDetail eventDetail = new ThingsObjectDataEventsMessageDTO.EventDetail();
		eventDetail.setName("zs");
		ArrayList<ThingsObjectDataEventsMessageDTO.EventDetail> objects = new ArrayList<>();
		objects.add(eventDetail);

		thingsObjectDataEventsMessageDTO.setData(objects);
		new JavaFileSerializer().serialize(thingsObjectDataEventsMessageDTO);
		ThingsObjectDataEventsMessageDTO dd = new JavaFileSerializer().deserialize(null, ThingsObjectDataEventsMessageDTO.class);
		System.out.println(dd);
		System.out.println(dd.getData().get(0).getName());

	}


}
